package com.bskj.kuaifu.controller;

import java.util.Map;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.bskj.common.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.bskj.common.bean.model.UserInfo;
import com.bskj.common.web.BaseController;
import com.bskj.kuaifu.bean.vo.req.UserReqVo;
import com.bskj.kuaifu.cache.redis.string.PhoneCodeCache;
import com.bskj.kuaifu.controller.web.annotation.RegisterRequired;
import com.bskj.kuaifu.service.IUserInfoService;

@Controller
@RequestMapping("register")
public class RegisterController extends BaseController {

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private PhoneCodeCache phoneCodeCache;

    @RegisterRequired
    @RequestMapping("page")
    public ModelAndView page(String openId, String appId, String jsonWechatUser) throws Exception {

        LogUtils.debug(JSONObject.toJSONString(getRequest().getParameterMap()));
        ModelAndView mv = new ModelAndView();
        mv.addObject("openId", openId);
        mv.addObject("appId", appId);
        mv.addObject("jsonWechatUser", jsonWechatUser);

        JSONObject params = new JSONObject();
        params.put("uri", "/register/verifyNo");
        params.put("appId", appId);
        params.put("openId", openId);
        mv.addObject("verifyNoParams", EncryptUtil.encrypt(params.toJSONString()));

        JSONObject regParams = new JSONObject();
        regParams.put("uri", "/register/reg");
        regParams.put("appId", appId);
        regParams.put("openId", openId);
        mv.addObject("regParams", EncryptUtil.encrypt(regParams.toJSONString()));

        JSONObject addCarNoParams = new JSONObject();
        addCarNoParams.put("uri", "/userCar/addCarNo");
        addCarNoParams.put("appId", appId);
        addCarNoParams.put("openId", openId);
        mv.addObject("addCarNoParams", EncryptUtil.encrypt(addCarNoParams.toJSONString()));

        String userId = getRequest().getParameter("userId");
        if (!StringUtils.isBank(userId)) {
            mv.addObject("userId", userId);
            JSONObject userInfoParams = new JSONObject();
            userInfoParams.put("uri", "/user/info");
            userInfoParams.put("userId", userId);
            userInfoParams.put("appId", appId);
            userInfoParams.put("openId", openId);
            mv.addObject("userInfoParams", EncryptUtil.encrypt(userInfoParams.toJSONString()));
        }
        mv.setViewName("/user/register");
        return mv;
    }

    /**
     * 获取手机验证码
     *
     * @param phoneNo
     * @return
     */
    @ResponseBody
    @RequestMapping("verifyNo")
    public String getVerifyNo(String phoneNo) throws Exception {
        if (!StringUtils.isBank(phoneNo)) {
            UserInfo userInfo = userInfoService.getUserInfoByPhone(phoneNo);
            if (userInfo != null) {
                return "号码已经存在，请重新输入";
            }
        }
        String verifyNo = SmsUtils.createRandomVcode();
        phoneCodeCache.setEx(phoneNo, Integer.parseInt(Global.getConfig("VERIFYNO_EXPIRE_TIME")), verifyNo);
        SendSmsResponse sendSmsResponse = SmsUtils.sendRegisterCode(verifyNo,phoneNo);
        LogUtils.debug("verifyNo:" + verifyNo);
        if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
            return "";
        }else{
            return "发送短信失败，请联系管理员";
        }
    }

    /**
     * 绑定手机,车牌
     *
     * @param userReqVo
     * @return
     */
    @ResponseBody
    @RequestMapping("reg")
    public Map<String, Object> register(UserReqVo userReqVo) {
        Map<String, String> resultMap = null;
        try {
            LogUtils.debug(userReqVo);
            resultMap = userInfoService.register(userReqVo);
            if (StringUtils.isBank(resultMap.get("error"))) {
                return ResultMapUtil.genDefaultSuccessMap();
            } else {
                return ResultMapUtil.genErrorMap(resultMap.get("error"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMapUtil.genErrorMap("绑定失败");
        }
    }


}
